package com.google.code.netbuilder.dal.providers.jpa;

import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import com.google.code.netbuilder.dal.entities.EquipmentTemplate;
import com.google.code.netbuilder.dal.providers.IEquipmentTemplateProvider;

@Stateless
public class EquipmentTemplateProviderJPA implements IEquipmentTemplateProvider{
	
	@PersistenceContext(unitName = "NetBuilder")
    EntityManager em;

	public EquipmentTemplateProviderJPA(){
		
	}
	
	@Override
	public void save(EquipmentTemplate entity) throws RuntimeException {
		em.persist(entity);
	}

	@Override
	public void update(EquipmentTemplate entity) throws RuntimeException {
		em.merge(entity);
	}

	@Override
	public void delete(EquipmentTemplate entity) throws RuntimeException {
		em.remove(entity);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<EquipmentTemplate> findAll() throws RuntimeException {
		List<EquipmentTemplate> res;
		res = (List<EquipmentTemplate>) em.createQuery("select e from EquipmentTemplate e").getResultList();
		return res;
	}

	@Override
	public EquipmentTemplate findByID(Integer id) throws RuntimeException {
		Query q = em.createQuery("select e from EquipmentTemplate e where e.id = :id");		
		return (EquipmentTemplate)q.setParameter("id", id).getSingleResult();
	}

	@Override
	public EquipmentTemplate findByName(String name) {
		Query q = em.createQuery("select e from EquipmentTemplate e where e.name = :name");		
		return (EquipmentTemplate)q.setParameter("name", name).getSingleResult();
	}

}
